package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.dto.RePassVO;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * @version 1.0
 * @Author 杨旭康
 * @Date 2022/10/30 11:43
 * @question:
 */

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * 请求 URL: http://localhost:8080/employee/login
     * 请求方法: POST
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request , @RequestBody Employee employee) {

        //密码是采用md5加密处理的(所以先获取前端用户输入的密码加密)
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //调用service将条件信息放进去查询获取一个用户信息
        Employee emp = employeeService.login(employee);

        if (emp == null) {
            return R.error("用户名不正确请重新输入");
        }

        if (! emp.getPassword().equals(password)) {
            return R.error("密码输入错误请重新输入");
        }

        if (emp.getStatus() == 0) {
            return R.error("此用户已被禁用~");
        }

        //走到这一步 证明都没事 所以将emp的id存入session域中(后边判断用户是否登录会用到)
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 用户退出
     * 请求 URL: http://localhost:8080/employee/logout
     * 请求方法: POST
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logOut(HttpServletRequest request) {

        //无需传别的参数 只是退出功能
        HttpSession session = request.getSession();
        session.invalidate();
        return R.success("用户退出成功~");
    }

    /**
     * 添加员工
     * 请求 URL: http://localhost:8080/employee
     * 请求方法: POST
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee) {

        //暂时将创建信息,更新时间等设置在此
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateTime(LocalDateTime.now());

        //调用service添加
        employeeService.saveEmp(employee);

        return R.success("添加成功~");
    }

    /**
     * 员工信息分页条件查询
     * 请求 URL: http://localhost:8080/employee/page?page=1&pageSize=2&name=123
     * 请求方法: GET
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageR(int page,int pageSize,String name) {

        log.info("page = {},pageSize = {},name = {}",page,pageSize,name);

        Page<Employee> pageInfo = employeeService.getByPage(page, pageSize, name);

        return R.success(pageInfo);
    }

    /**
     * 修改员工信息(状态/编辑等)
     * 后台向前端传id精度会减小-->解决 将Long类型数据转为字符串不会再有精度减少问题
     * 这样修改的话不会修改成功,id是19位的雪花算法,而js只能处理精度16位的数字,加入消息处理器即可解决
     * 请求 URL: http://localhost:8080/employee
     * 请求方法: PUT
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee) {

        log.info(employee.toString());

        //修改之后 要记着保留修改人的id和修改时间
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(empId);
//        employee.setUpdateTime(LocalDateTime.now());

        employeeService.updateByID(employee);

        return R.success("员工信息修改成功~");
    }

    /**
     * 修改员工--回显数据通过id
     * 请求 URL: http://localhost:8080/employee/1584900510974013441
     * 请求方法: GET
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {

        log.info("员工id 为: {}",id);

        Employee emp = employeeService.getById2(id);

        if (null != emp) {
            //判断一下防止被删除不知道
            return R.success(emp);
        }
        return R.error("操作失败,请刷新页面重试");
    }

    /**
     * 修改密码
     * 请求 URL: http://localhost:8080/employee/repass
     * 请求方法: PUT
     * @param request
     * @param rePassVO
     * @return
     */
    @PutMapping("/repass")
    public R<String> updateByMm(HttpServletRequest request, @RequestBody RePassVO rePassVO) {

        //对前端传过来的原始密码 加密处理
        String password = DigestUtils.md5DigestAsHex(rePassVO.getPassword().getBytes());
        //获取当前登录人
        Long empId = (Long) request.getSession().getAttribute("employee");
        Employee emp = employeeService.getById(empId);

        //1.判断输入的密码是否正确
        if (!emp.getPassword().equals(password)) {
            return R.error("您输入的原始密码不对,请重新输入");
        }
        //2.判断输入的 和 二次输入新密码是否一样
        if ( (null == rePassVO.getNewPassword() || "".equals(rePassVO.getNewPassword()) || null == rePassVO.getNewPassword2() || "".equals(rePassVO.getNewPassword2())) ) {
            return R.error("密码不能为空,且不能低于6位");
        }

        if (!rePassVO.getNewPassword().equals(rePassVO.getNewPassword2())) {
            return R.error("两次输入的新密码不一致,请重新输入");
        }
        //3.判断老密码和新密码是否一样
        String newPassword2 = rePassVO.getNewPassword2();
        String s = DigestUtils.md5DigestAsHex(newPassword2.getBytes());
        if (emp.getPassword().equals(s)) {
            return R.error("两次输入的密码不能一致哦");
        }
        //4.修改成新密码,且修改更新人和更新时间,并调用service修改
        emp.setPassword(DigestUtils.md5DigestAsHex(rePassVO.getNewPassword().getBytes()));
        emp.setUpdateTime(LocalDateTime.now());
        emp.setUpdateUser(emp.getId());
        employeeService.updateById(emp);

        return R.success("密码重置成功");
    }








}
